package ferp.android.engagement;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import ferp.core.log.Log;

import java.util.Date;

/**
 * User: olegs
 * Date: 12/7/12 10:49 PM
 */
public class ApplicationRater
{
  // dialog option indexes
  public static final int RATE_NOW   = 0;
  public static final int RATE_LATER = 1;
  public static final int RATE_NEVER = 2;

  public static void onAppLaunch(Activity activity, int dialogID)
  {
    // get application object
    Application application = activity.getApplication();
    // get rater's shared preference
    SharedPreferences sp = application.getSharedPreferences(Key.RATER, Context.MODE_PRIVATE);
    // read last date
    Date lastDate = new Date(sp.getLong(Key.LAST_DATE, 0));
    // get current time
    Date now = new Date(System.currentTimeMillis());
    // state
    State state = State.values()[sp.getInt(Key.STATE, State.WAIT.ordinal())];

    Log.debug(Log.TAG, "current date: " + now + ", last launch date: " + lastDate + ", state: " + state);

    switch (state)
    {
      case WAIT:
        // update launches count once in a day
        if (now.getYear() != lastDate.getYear() || now.getMonth() != lastDate.getMonth() || now.getDate() != lastDate.getDate())
        {
          // read launches count
          int launchesCount = sp.getInt(Key.LAUNCHES_COUNT, 0) + 1;
          SharedPreferences.Editor editor = sp.edit();

          Log.debug(Log.TAG, "launches count: " + launchesCount + " out of " + LAUNCHES_UNTIL_FIRST_PROMPT);

          // save updated launches count
          editor.putInt(Key.LAUNCHES_COUNT, launchesCount);
          // save last date
          editor.putLong(Key.LAST_DATE, now.getTime());

          if (launchesCount == LAUNCHES_UNTIL_FIRST_PROMPT)
          {
            // update state
            editor.putInt(Key.STATE, State.REMIND.ordinal());
            // show the rate dialog at the first time
            activity.showDialog(dialogID);
          }

          editor.commit();
        }

        break;

      case REMIND:
        // remind an user to rate the application
        if (now.getTime() - lastDate.getTime() >= REMIND_PERIOD)
          activity.showDialog(dialogID);

        break;
    }
  }

  public static void onOptionSelected(Activity activity, int option)
  {
    // get application object
    Application application = activity.getApplication();
    // get rater's shared preference
    SharedPreferences.Editor editor = application.getSharedPreferences(Key.RATER, Context.MODE_PRIVATE).edit();

    switch (option)
    {
      case RATE_NOW:
        // go to Google Play
        activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + application.getPackageName())));
        editor.putInt(Key.STATE, State.RATED.ordinal());
        break;

      case RATE_LATER:
        editor.putInt(Key.STATE, State.REMIND.ordinal());
        // update last date
        editor.putLong(Key.LAST_DATE, System.currentTimeMillis());
        break;

      case RATE_NEVER:
        editor.putInt(Key.STATE, State.DENIED.ordinal());
        break;
    }

    editor.commit();
  }

  // for debug
  public static void clear(Activity activity)
  {
    SharedPreferences.Editor editor = activity.getApplication().getSharedPreferences(Key.RATER, Context.MODE_PRIVATE).edit();

    editor.clear();
    editor.commit();
  }

  // number of launches before the dialog is shown at the first time
  private static final int LAUNCHES_UNTIL_FIRST_PROMPT = 3;
  // remind period
  private static final long REMIND_PERIOD = 2 * 24 * 60 * 60 * 1000; // 2 days

  // state of the rater
  private static enum State {WAIT, REMIND, DENIED, RATED}

  // preference keys
  private static final class Key
  {
    private static final String RATER          = "rater";
    private static final String STATE          = "rater_state";
    private static final String LAUNCHES_COUNT = "rater_launches_count";
    private static final String LAST_DATE      = "rater_last_date";
  }
}
